package gui;

import javax.swing.*;
import javax.swing.table.*;
import database.ProductOperations;
import java.awt.*;
import java.util.Vector;

@SuppressWarnings("serial")
public class Butter extends JPanel 
{
	public static JTable table;
	public static DefaultTableModel model;
	private JPanel p1;
	private static ProductOperations po;
	
	public Butter() {
		
		this.setPreferredSize(new Dimension(720, 260));
		p1=new JPanel();
		p1.setPreferredSize(new Dimension(720, 260));
		// create blank JTable on screen
		String[] cols={"ID","PRODUCT NAME","PRODUCT SUPPLIER","PRICE"};
		int numRows=100;
		model=new DefaultTableModel(numRows,cols.length);
		model.setColumnIdentifiers(cols);
		table = new JTable(model);
		table.setFillsViewportHeight(true);
		table.setAutoCreateColumnsFromModel(false);
		JScrollPane sp=new JScrollPane(table);
		sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		sp.setPreferredSize(new Dimension(700, 250));
				
		//adjust size of columns on table
		TableColumnModel tbm=table.getColumnModel();
		tbm.getColumn(0).setMaxWidth(30);
		tbm.getColumn(3).setMaxWidth(100);
		model.setRowCount(0);
		
		p1.add(sp,BorderLayout.CENTER);
		this.add(p1);
		display();
	}
	//display products in table
	public void display(){
		
		int counter = 0;
		for(int i=0;i<tab6.prods.size();i++){
			
			if(tab6.prods.get(i).getCategory().equals("Butter")){
				
				Vector<Object> row = new Vector<Object>();
				row.add(tab6.prods.get(i).getId());
				row.add(tab6.prods.get(i).getName());
				row.add(tab6.prods.get(i).getSupplier());
				row.add(tab6.prods.get(i).getPrice());
				model.insertRow(counter,row);
				counter++;
			}
		}
	}
		//update display products in table
		public static void update(){
			
			int counter = 0;
			po = new ProductOperations();
			tab6.prods = po.getProductList();
			model = (DefaultTableModel) table.getModel();
			model.setRowCount(0);
			
			for(int i=0;i<tab6.prods.size();i++){
				
				if(tab6.prods.get(i).getCategory().equals("Butter")){
					
					Vector<Object> row = new Vector<Object>();
					row.add(tab6.prods.get(i).getId());
					row.add(tab6.prods.get(i).getName());
					row.add(tab6.prods.get(i).getSupplier());
					row.add(tab6.prods.get(i).getPrice());
					model.insertRow(counter,row);
					counter++;
				}
			}
			
		}
}
